package com.ruoyi.boat.ws;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.boat.domain.*;
import com.ruoyi.boat.tcp.cache.MyApplicationCache;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WebsocketService {

    @Autowired
    private WebSocket webSocket;

    public static final String WAVE_DATA = "waveData";
    public static final String METEOR_DATA = "meteorData";
    public static final String WATER_QUALITY = "waterQuality";
    public static final String ATMOS_DATA = "atmosData";
    public static final String RADAR_DATA = "radarData";
    public static final String AIS_DATA = "aisData";
    public static final String CTD_DATA = "ctdData";
    public static final String ADCP_DATA = "adcpData";
    public static final String RESPONSE_DATA = "responseData";

    /**
     * 发送给全体
     *
     * @param object
     */
    private void sendAll(WebSocketObject object) {
        webSocket.sendAll(JSON.toJSONString(object));
    }

    private void send(String type, Object object) {
        WebSocketObject webSocketObject = new WebSocketObject(type, object);
        this.sendAll(webSocketObject);
    }

    public void sendNotification(String type) {
        WebSocketObject webSocketObject = new WebSocketObject(type, null);
        this.sendAll(webSocketObject);
    }

    public void sendToCode(String boatCode, String type, Object object) {
        WebSocketObject webSocketObject = new WebSocketObject(type, object);
        webSocket.sendByBoatCode(boatCode, JSON.toJSONString(webSocketObject));
    }

    /**
     * 产生报警的时候发送
     *
     * @param data
     */
    public void sendAlert(Alert data) {
        sendToCode(data.getBoatCode(), "alert", data);
    }

    /**
     * 收到状态的时候发送
     *
     * @param data
     */
    public void sendStatus(Status data) {
        sendToCode(data.getBoatCode(), "status", data);
    }

    /**
     * 收到气象数据后实时转发
     *
     * @param data
     */
    public void sendMeteorData(MeteorData data) {
        sendToCode(data.getBoatCode(), METEOR_DATA, data);
    }

    /**
     * 收到波浪数据后实施转发
     *
     * @param data
     */
    public void sendWaveData(WaveData data) {
        sendToCode(data.getBoatCode(), WAVE_DATA, data);
    }

    /**
     * 收到水质数据后实时转发
     *
     * @param data
     */
    public void sendWaterQuality(WaterQuality data) {
        sendToCode(data.getBoatCode(), WATER_QUALITY, data);
    }

    /**
     * 收到大气数据后实时转发
     *
     * @param data
     */
    public void sendAtmosData(AtmosData data) {
        sendToCode(data.getBoatCode(), ATMOS_DATA, data);
    }

    public void sendRadarData(RadarData data) {
//        sendToCode(data.getBoatCode(), RADAR_DATA, data);
    }

    public void sendAisData(AisData data) {
//        sendToCode(data.getBoatCode(), AIS_DATA, data);
    }

    public void sendCtdData(CtdData data) {
//        sendToCode(data.getBoatCode(), CTD_DATA, data);
    }

    public void sendAdcpData(AdcpData data) {
//        sendToCode(data.getBoatCode(), ADCP_DATA, data);
    }

    public void sendCmdResponse(MyApplicationCache.CmdRequest data){
        sendToCode(data.getBoatCode(),RESPONSE_DATA,data);
    }
}
